package com.xw.util;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author wxx
 * @version 1.0
 * @date 2020/4/2 16:08
 */
@Data
public class ResponseData implements Serializable {

    private static final String SUCCESS_MSG = "操作成功";
    private static final String ERROR_MSG = "操作失败";

    /**
     * 成功标识 true/false
     */
    private boolean status;
    /**
     * 返回的状态码
     */
    private int code;
    /**
     * 返回的信息
     */
    private String msg;
    /**
     * 返回的数据
     */
    private Object data;

    public ResponseData(boolean status, int code, String msg) {
        this.status = status;
        this.code = code;
        this.msg = msg;
    }

    public ResponseData(boolean status, int code, String msg, Object data) {
        this.status = status;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResponseData success(){
        return new ResponseData(true,200, ResponseData.SUCCESS_MSG);
    }

    public static ResponseData success(Object data){
        return new ResponseData(true,200, ResponseData.SUCCESS_MSG,data);
    }

    public static ResponseData success(String msg, Object data){
        if (StringUtils.isBlank(msg)){
            msg = ResponseData.SUCCESS_MSG;
        }
        return new ResponseData(true,200,msg,data);
    }

    public static ResponseData fail(){
        return new ResponseData(false,500, ResponseData.ERROR_MSG);
    }

    public static ResponseData fail(Object data){
        return new ResponseData(false,500, ResponseData.ERROR_MSG,data);
    }

    public static ResponseData fail(String msg, Object data){
        return new ResponseData(false,500,msg,data);
    }

    public static ResponseData fail(int code, String msg){
        return new ResponseData(false,code,msg);
    }

    public static ResponseData fail(int code, String msg, Object data){
        return new ResponseData(false,code,msg,data);
    }

}
